<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use think\Db;
use think\Session;

class Deposit extends IndexBase
{
	public function index(){

        $this->titleName = "提现";
        return $this->fetch();
    }
    public function card(){

        if( request()->isAjax() ){
            $d = input('post.');
            $c = Session::get('verify_code');
            if($d['d'] != $c){
                $this->json_return(1);
            }
            $name = Db::name('bank_card')->where('user_id',$this->userInfo['id'])->find();

            if( !empty($name) ){
                if($d['n'] != $name['name']){
                    $this->json_return(2);
                }
                if($d['c'] == $name['card_num']){
                    $this->json_return(3);
                }
            }

            $datas = array(
                'name' => $d['n'],
                'bank' => $d['b'],
                'card_num' => $d['c'],
                'mobile' => $d['m'],
                'user_id' => $this->userInfo['id'],
                "branch" => $d["branch"],
                'addtime' => time(),
                'status' => 5
            );
            $data = Db::name('bank_card')->insert($datas);
            if($data){
                $arr = [
                    'result' => 0,
                    'msg' => "添加成功",
                ];
            }else{
                $arr = [
                    'result' => 1009,
                    'msg' => "添加失败",
                ];
            }
            $this->json_return($arr);
        }
        $this->titleName = "添加银行卡";
        return $this->fetch();
    }

    public function card_code(){

        if( request()->isAjax() ){
            $mobile = input('post.mobile');
            $code = rand(100000,999999);
            $sms = app_service("common","TransmitSms");
            $qq = $sms->phone_validate($mobile,$code);
            if($qq){
                Session::set('verify_code',$code);
                $array = [
                    'result' => 0,
                    'msg' => "发送成功",
                ];
            }else{
                $array = [
                    'result' => 1004,
                    'msg' => "发送失败",
                ];
            }
            $this->json_return($array);
        }
    }

    public function cash(){

        $list = Db::name('bank_card')->where( array("user_id"=>$this->userInfo['id'],"status"=>5 ))->select();
        $cash = Db::name('user')->alias("a")
        		->join("ysy_user_info b","a.id = b.user_id","LEFT")
        		->where('id',$this->userInfo['id'])
        		->field('b.money,a.pay_pwd,a.pay_code')
        		->find();
        $double = Db::name('double')->where('id',4)->field('double')->find();

        if(request()->isAjax()){
            $d = input('post.');

            $time = time();
            /*$week = date('w', $time);
            if( $week != 5 ){
                $arr = [
                    'result' => 1058,
                    'msg' => "每周五出售奖金积分",
                ];
                $this->json_return($arr);
            }*/

            if(getMd5Pas($d['cash_paw'],$cash['pay_code']) != $cash['pay_pwd']){
                $arr = [
                    'result' => 1059,
                    'msg' => "你提现的资金密码有误",
                ];
                $this->json_return($arr);
            }

            $c = $d['money'] % $double["double"];
            if( $c !== 0){
                $this->json_return(1044);
            }

            if($cash['money'] < $d['money']){
                $this->json_return(1057);
            }

            $today = strtotime(date("Y-m-d"),$time);

            $pd = Db::name("cash_record")->field("id")->where(array("cash_time"=>["gt",$today],"user_id"=>$this->userInfo["id"]))->find();
            /*if( !empty($pd) ){
                $arr = [
                    'result' => 1057,
                    'msg' => "每周只能出售奖金积分一次",
                ];
                $this->json_return($arr);
            }*/

            $data = array(
                'bank_card_id' => $d['bank'],
                'money' => $d['money'],
                'status' => 0,
                'user_id' => $this->userInfo['id'],
                'cash_time' => $time
            );

            $result1 = Db::name('cash_record')->insert($data);

            //提现成功，修改用户表的金额
            $list = array(
                'user_id' => $this->userInfo['id'],
                'money' => $cash['money'] - $d['money']
            );
            $result2 = Db::name('user_info')->update($list);
            if($result1 && $result2){
                $arr = [
                    'result' => 0,
                    'msg' => "提现成功，等待审核",
                ];
            }else{
                $arr = [
                    'result' => 1005,
                    'msg' => "提现失败",
                ];
            }
            $this->json_return($arr);
        }

        $this->assign("double",$double);
        $this->assign("cash",$cash);
        $this->assign("list",$list);
        return $this->fetch();
    }


    public function tixian_record(){

        $data = Db::name("cash_record")
            ->alias("cr")
            ->join("ysy_bank_card bc","bc.id = cr.bank_card_id")
            ->where("cr.user_id =".$this->userInfo['id'])
            ->field("cr.status,cr.cash_time,cr.money,bc.card_num,bc.name,bc.bank")
            ->select();

        $this->assign('data',$data);
        return $this->fetch();
    }


    public function card_list(){

        $list = Db::name("BankCard")->where(array("user_id"=>$this->userInfo['id'],"status"=>5 ))->select();


        $this->assign('list',$list);
        return $this->fetch();
    }

    public function del_card(){
        if( request()->isAjax() ){
            $id = input("post.id");
            if( empty($id) ){
                $retArr = [
                    'result' => 1,
                    'msg' => "删除有误",
                ];
                $this->json_return($retArr);
            }
            $pd = Db::name("BankCard")->where("id",$id)->update(array("status"=>0));
            if( $pd ){
                $retArr = [
                    'result' => 0,
                    'msg' => "删除成功",
                ];
            }else{
                $retArr = [
                    'result' => 1,
                    'msg' => "删除失败",
                ];
            }
            $this->json_return($retArr);
        }
    }
}	